package com.user.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.user.domain.entity.User;
import com.user.domain.repository.UserRepository;
import com.user.infrastructure.entity.UserDao;
import com.user.infrastructure.repository.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import static com.user.domain.factory.UserFactory.USER_CONVERT;

/**
 * @author 大忽悠
 * @create 2022/9/15 21:36
 */
@Repository
@RequiredArgsConstructor
public class UserRepositoryImpl implements UserRepository {
    private final UserMapper userMapper;

    @Override
    public Integer registerUser(User user) {
        UserDao userDao = USER_CONVERT.toUserDao(user);
        userMapper.insert(userDao);
        return userDao.getId();
    }

    @Override
    public void updateUserInfo(User user) {

    }

    @Override
    public void deleteUser(Integer id) {

    }

    @Override
    public User getUser(String username, String password) {
        return USER_CONVERT.toUser(userMapper.selectOne(new QueryWrapper<UserDao>().eq("username", username)
                .eq("password", password)));
    }

    @Override
    public User getUser(String username) {
        return USER_CONVERT.toUser(userMapper.selectOne(new QueryWrapper<UserDao>().eq("username", username)));
    }
}
